<?php

/*
 *  Action $3
 * 
 *  Trash a card from your hand.
 *  Gain a card costing exactly $1 more than it and a card costing exactly
 *  $1 less than it, in either order, putting them on top of your deck.
 * 
 */

class Develop extends Card implements iAction, iKingdom, iHinterlands {

    public function __construct() {
        parent::__construct("Develop", 3);
    }

    public function getDescription() {
        return array(
            "Trash a card from your hand.",
            "Gain a card costing exactly $1 more than it and a card costing exactly $1 less than it, in either order, putting them on top of your deck.");
    }

    public function play(Game $game) {

        $playhand = $game->getPlayHand();
        if (sizeof($playhand) > 0) {
            $playhand = getUniqueCards($playhand);
            if (sizeof($playhand) > 1) {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $playhand, 1, 1, null, "Choose a card to trash");
                $trashedCardName = $output[0];
            }
            else {
                $trashedCardName = $playhand[0];
            }
            $trashedCard = $game->moveCardFromPileToPile($trashedCardName, PlayHand, Trash);
            ScreenDisplay::addScreenMessage($game, true, "trash " . nicePrint($trashedCard), "trashes " . ScreenDisplay::$LMSG);

            if ($trashedCard == null) {
                return true;
            }

            // get cards you can gain
            $trashedPrice = $trashedCard->getPrice($game);
            $price1 = $trashedPrice->add(1);


            $gainedCards = array();
            $optional_cards1 = $game->getBoardCardsByPrice($price1, $price1);
            if (sizeof($optional_cards1) > 0) {
                if (sizeof($optional_cards1) > 1) {
                    $output = MySocket::GetUserReply($game, Input_ChooseOne, $optional_cards1, 1, 1, null, "Choose a card to gain");
                    $new_card_name = $output[0];
                    $gainedCards[] = n2c($new_card_name);
                } else {
                    $gainedCards[] = $optional_cards1[0];
                }
            }
            if ($trashedPrice->getCoins() > 0) {
                $price2 = $trashedPrice->add(-1);
                $optional_cards2 = $game->getBoardCardsByPrice($price2, $price2);
                if (sizeof($optional_cards2) > 0) {
                    if (sizeof($optional_cards2) > 1) {
                        $output = MySocket::GetUserReply($game, Input_ChooseOne, $optional_cards2, 1, 1, null, "Choose a card to gain");
                        $new_card_name = $output[0];
                        $gainedCards[] = n2c($new_card_name);
                    } else {
                        $gainedCards[] = $optional_cards2[0];
                    }
                }
            }


            if (sizeof($gainedCards) > 0) {
                $output = MySocket::GetUserReply($game, Input_Reorder, $gainedCards, -1, -1, null, "Put cards on top of your deck in any order");
                ScreenDisplay::addScreenMessage($game, true, "put " . nicePrint($output) . " on top of your deck", "puts " . ScreenDisplay::$LMSG . " on top of his deck");
                $output = array_reverse($output);
                foreach ($output as $p) {
                    $game->moveCardFromPileToPile($p, BoardCards, DrawPile);
                }
            }
            $game->cardActionFinished();
            return true;
        }
    }

}

?>